﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TANKWAR_alpha
{
    class Collision
    {
        private GameGDI gameEngine; 

        public  GameGDI GameEngine
        {
            get { return gameEngine; }
        }

        public Collision(GameGDI gGDI)
        {
            gameEngine = gGDI;
        }
        /*
         *every function's duty is explained by the fuction name, 
         * unneccesarily to comment 
         */
        public bool IsPlayerBulletsHitTankRobot(Bullet b)
        {
            foreach(TankRobot taR in gameEngine.TankRobots)
            {
                if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, taR.Left, taR.Top, Constant.tankWidth, Constant.tankHeight))
                {
                    taR.Life = false;
                    return true;
                }
            }
             return false;
        }

        public bool IsRobotBulletsHitTankPlayer(Bullet b)
        {
            if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, gameEngine.MyTank.Left, gameEngine.MyTank.Top, Constant.tankWidth, Constant.tankHeight))
            {
                return true;
            }
            
            return false;
        }

        public int IsPlayerBulletsHitReWall(Bullet b)
        {
            foreach(Wall wa in gameEngine.Walls)
            {
                if (wa.WallType == 0 || wa.WallType == 1||wa.WallType==4)
                {
                    if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, wa.WallLeft, wa.WallTop, Constant.WallWidth, Constant.WallHeight))
                    {
                        if (wa.WallType == 0)
                        {
                            wa.WallLife = false;
                        }
                        if (wa.WallType == 4)
                        {
                            //wa.WallLife = false;
                            return 2;
                        }
                        return 1;
                    }
                }
            }
            return 0;
        }

        public int IsRobotBulletsHitRewall(Bullet b)
        {
            foreach (Wall wa in gameEngine.Walls)
            {
                if (wa.WallType == 0 || wa.WallType == 1 || wa.WallType == 4)
                {
                    if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, wa.WallLeft, wa.WallTop, Constant.WallWidth, Constant.WallHeight))
                    {
                        if (wa.WallType == 0)
                        {
                            wa.WallLife = false;
                        }
                        if (wa.WallType == 4)
                        {
                            //wa.WallLife = false;
                            return 2;
                        }
                        return 1;
                    }
                }
            }
            return 0;
        }

        public bool IsPlayerBulletHitRobotBullets(Bullet b)
        {
            foreach(Bullet roB in gameEngine.RobotBullets)
            {
                if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, roB.Left, roB.Top, Constant.bombWidth, Constant.bombHeight))
                {
                    roB.Life = false;
                    return true;
                }
            }
            return false;
        }

        public bool IsRobotBulletsHitPlayerBullets(Bullet b)
        {
            foreach (Bullet roB in gameEngine.PlayerBullets)
            {
                if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, roB.Left, roB.Top, Constant.bombWidth, Constant.bombHeight))
                {
                    roB.Life = false;
                    return true;
                }
            }
            return false;
        }

        public bool IsTankHitBullets(ObjElement tank)
        {
            if (tank is TankRobot)
            {
                foreach (Bullet b in gameEngine.PlayerBullets)
                {
                    if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, tank.Left, tank.Top, Constant.tankWidth, Constant.tankHeight))
                    {
                        b.Life = false;
                        return true;
                    }
                }
                return false;
            }
            else
            {
                foreach (Bullet b in gameEngine.PlayerBullets)
                {
                    if (Constant.RectangleIntersection(b.Left, b.Top, Constant.bombWidth, Constant.bombHeight, tank.Left, tank.Top, Constant.tankWidth, Constant.tankHeight))
                    {
                        b.Life = false;
                        return true;
                    }
                }
                return false;
            }
        }

        public bool BornInBlankGround(int left,int top)
        {
            foreach(Wall w in gameEngine .Walls)
            {
                if (Constant.RectangleIntersection(left, top, Constant.tankWidth, Constant.tankHeight, w.WallLeft, w.WallTop, Constant.WallWidth, Constant.WallHeight))
                    return false;
            }

            foreach(TankRobot taR in gameEngine .TankRobots)
            {
                if (Constant.RectangleIntersection(left, top, Constant.tankWidth, Constant.tankHeight, taR.Left, taR.Top, Constant.tankWidth, Constant.tankHeight))
                    return false;
            }

            if (Constant.RectangleIntersection(left, top, Constant.tankWidth, Constant.tankHeight, gameEngine.MyTank.Left, gameEngine.MyTank.Top, Constant.tankWidth, Constant.tankHeight))
                return false;

            return true;
        }
    }
}
